package data;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;

public class MySql {
	String host;
	String user;
	String pass;
	String database;
	Connection con;
	protected Statement stmt;

	public MySql(String h, String u, String p, String d) throws SQLException {
		host = h;
		user = u;
		pass = p;
		database = d;

		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			System.out.println("Driver jdbc non rilevato, installarlo");
			return;
			// e.printStackTrace();
		}

	}

	public void connect() throws SQLException {
		con = DriverManager.getConnection("jdbc:mysql://" + host + ":3306/"
				+ database, user, pass);
		stmt = con.createStatement(); // TODO ottimizzare il tipo di statement

	}

	public LinkedList<String> query(String query, String column)
			throws SQLException {
		ResultSet result = stmt.executeQuery(query);
		LinkedList<String> defs = new LinkedList<String>();
		while (result.next()) {
			defs.add(result.getString(column));
		}
		return defs;
	}
}
